package net.dao.impl;

import java.util.List;

import net.dao.GridAndBackgroundDao;
import net.form.processing.HSVRangeGridAndBackground;
import net.model.GridAndBackground;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository(value = "gridAndBackgroundDao")
public class GridAndBackgroundDaoImpl implements GridAndBackgroundDao {

	@Autowired
	private SessionFactory sessionFactory;

	public GridAndBackground getGridAndBackground() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				GridAndBackground.class);
		return (GridAndBackground) criteria.list().get(0);
	}

	public double getSizeGridReference() {
		GridAndBackground gGridAndBackground = getGridAndBackground();
		return gGridAndBackground.getGrid();
	}
	public void setSizeGridReference(double longitud) {
		GridAndBackground gridReference = getGridAndBackground();
		gridReference.setGrid(longitud);
		sessionFactory.getCurrentSession().saveOrUpdate(gridReference);
	}

	public void setHSVRangeGridAndBackground(
			HSVRangeGridAndBackground rangeGridAndBackground) {
		GridAndBackground gridAndBackground = getGridAndBackground();
		gridAndBackground.setHSVRangeGridAndBackground(rangeGridAndBackground);
		sessionFactory.getCurrentSession().saveOrUpdate(gridAndBackground);

	}

	public GridAndBackground findConfiguracion(String nombreSistema) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				GridAndBackground.class);
		List<GridAndBackground> list = (List<GridAndBackground>) criteria.add(
				Restrictions.eq("systemName", nombreSistema)).list();
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	public void setSeparationOfObjects(boolean separationOfObjects) {
		GridAndBackground gridReference = getGridAndBackground();
		gridReference.setSeparationOfObjects(separationOfObjects);
		sessionFactory.getCurrentSession().saveOrUpdate(gridReference);
	}

}
